#!/bin/bash

#
# 
# Copyright (c) 2007-2013, University of California / Singapore Management University
#   Lingxiao Jiang         <lxjiang@ucdavis.edu> <lxjiang@smu.edu.sg>
#   Ghassan Misherghi      <ghassanm@ucdavis.edu>
#   Zhendong Su            <su@ucdavis.edu>
#   Stephane Glondu        <steph@glondu.net>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
#

TOOVERWRITE=

clean()
{
	echo -n "...deleting intermediate vector files..."
	find "$SRC_DIR" -iname '*.vec' -print0 | xargs -0 -n 500 rm -f 
	echo "Done"
}

clean_log()
{
	echo -n "...deleting vgen logs..."
	find "$TIME_DIR" -iname 'vgen_*' -print0 | xargs -0 -n 500 rm -f 
	echo "Done"
	echo -n "...deleting html transforming logs..."
	rm -f "$TIME_DIR/`basename "$SRC2HTM_EXEC"`.log"
	echo "Done"
	echo -n "...deleting vector db files..."
	find "$VECTOR_DIR" -iname 'vdb_*' -print0 | xargs -0 -n 500 rm -f 
	echo "Done"
}

#vgen $1 $2: $1=min_token; $2=stride
vgen()
{
	if (($# != 2));
	then
		return 1
	fi

	echo -n "$FUNCNAME: $1 $2 ..."  # FUNCNAME: the current function name; maintained by Bash
	if [[ "$TOOVERWRITE" != "true" && -s "$VECTOR_DIR/vdb_$1_$2" ]];
	then
	  echo
	  echo "Warning: Vec file $VECTOR_DIR/vdb_$1_$2 exists. Delete it manually or use '$0 overwrite'."
	  echo "$FUNCNAME: $1 $2 ...Skip."
	else
	  echo -n > "$TIME_DIR/vgen_$1_$2"

#NOTE: "for .. in .." has trouble when a filename contains spaces; use "while read .." instead:
	  ( time \
	    find "$SRC_DIR" -ipath "*/$PDG_DIR/$FILE_PATTERN" | while read pdgfile;
#	    for pdgfile in `find "$SRC_DIR" -iname "$FILE_PATTERN"`;
	    do
		echo "Parsing $pdgfile ..."
		outfile=${pdgfile}.vec
		if [[ ! -s "${outfile}" || "$TOOVERWRITE" = "true" ]];
		then
			astfile=${pdgfile/$PDG_DIR/$AST_DIR}
			if [[ ! -s "$astfile" ]]; then
				echo "Missing AST dot file: $astfile. Skip."
				continue
			fi
			echo "$VGEN_EXEC" --type-file "$TYPE_FILE" --relevant-node-file "$RELEVANT_NODEFILE" --leaf-node-file "$LEAF_NODEFILE" --parent-node-file "$PARENT_NODEFILE" -p "$pdgfile" -a "$astfile" -m $1 -s $2 -o "$outfile"
			"$VGEN_EXEC" --type-file "$TYPE_FILE" --relevant-node-file "$RELEVANT_NODEFILE" --leaf-node-file "$LEAF_NODEFILE" --parent-node-file "$PARENT_NODEFILE" -p "$pdgfile" -a "$astfile" -m $1 -s $2 -o "$outfile"
		fi
	    done \
	  ) 1>>"$TIME_DIR/vgen_$1_$2" 2>&1

	  find "$SRC_DIR" -iname '*.vec' -print0 | xargs -0 -n 100 cat > "$VECTOR_DIR/vdb_$1_$2"
	  echo "Done. Log: $TIME_DIR/vgen_$1_$2"
        fi

	if [[ -s "$VECTOR_DIR/vdb_$1_$2" ]]; then
		return 0
	else
		echo "Warning: empty vector file for vgen $1 $2"
		return 1
	fi
}

. `dirname $0`/configure-dot

if [[ $# -ge 1 ]]; then
	case "$1" in
		clean )
			clean
			exit $?
			;;
		clean_all )
			clean
			clean_log
			exit $?
		       	;;
		overwrite )
			TOOVERWRITE="true"
			;;
		* )
			echo "Usage: $0 [overwrite | clean | clean_all]"
			exit 1
			;;
	esac
fi

errcode=1
echo "$VGEN_EXEC $SRC_DIR/$PDGFILES"
echo 

for t in ${MIN_TOKENS};
do
	for s in ${STRIDE};
	do
		vgen $t $s
		retcode=$?
		# return 0 if any vector gen is successful
		if [[ $retcode -eq 0 ]]; then
			errcode=0
		fi
		clean # remove .vec so not to intervene with the following vgens
		echo
	done
done

exit $errcode

